package com.caizi.socket.mail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class Send {

    // 签名
    private static final String signature = " <br> <br> <br>--------------<br>Thanks&Regards <br>孟玉峰北京长信通信息技术有限公司  实施事业部 <br>Mail: meng_yf@giantstone.com <br>Tel: 010-58272727-6789 <br>Mob: 15801478912";

    private static String content = "你好：<br><br>&nbsp;&nbsp;&nbsp;&nbsp;附件是你7月份考试结果，请查收。请问题请及时联系！<br>";

    private static String subject = "7月份考试结果";

    private static String cc = "li_py@giantstone.com"; // 抄送地址

    private static boolean hasAdjunct = true; // 是否有附件

    public static void main(String... strings) throws Exception {
        P2PEmail mail = new P2PEmail("孟玉峰", "meng_yf@giantstone.com", "mail.giantstone.com", "meng_yf", "lianfeng1201!", true);
        Map<String, String> receiveList = receiveNames();
        if (hasAdjunct) {
            Map<String, Vector<File>> recAdjuncts = recAdjuncts();
            for (String name : recAdjuncts.keySet()) {
                mail.setTo(receiveList.get(name));
                mail.setCc(cc);
                mail.setSubject(subject);
                mail.setContent(name + content + signature);
                mail.setFile(recAdjuncts.get(name));
                mail.send();
            }
        } else {
            for (String name : receiveList.keySet()) {
                mail.setTo(receiveList.get(name));
                mail.send();
            }
        }
    }

    /**
     * 批量收件人
     */
    private static Map<String, String> receiveNames() throws Exception {
        Map<String, String> receiveNames = new HashMap<String, String>();
        File file = new File("src/com/caizi/socket/mail/收件人");
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            while (br.ready()) {
                String line = br.readLine();
                String[] recs = line.split(" ");
                receiveNames.put(recs[0].trim(), recs[1].trim());
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != br) {
                br.close();
            }
        }
        return receiveNames;
    }

    /**
     * 获取获取附件
     */
    private static Map<String, Vector<File>> recAdjuncts() {
        Map<String, Vector<File>> recAdjuncts = new HashMap<String, Vector<File>>();
        File dir = new File("src/com/caizi/socket/mail/附件/");
        for (File file : dir.listFiles()) {
            String name = file.getName();
            String toName = name.substring(name.lastIndexOf("-") + 1, name.lastIndexOf("."));
            Vector<File> adjuncts = new Vector<File>();
            adjuncts.add(file);
            recAdjuncts.put(toName, adjuncts);
        }
        return recAdjuncts;
    }
}
